/**
	License:		Artistic license 2.0 (see license.txt, also available online at http://www.perlfoundation.org/artistic_license_2_0)
	Authors:		OrbitalLab (http://www.orbitallab.ru/moonglide/), 2007-2009

	File:			LogSystem.d
	Description:	loggers & public imports to use anywhere
	Date:			10.04.2008 by Digited
**/
module moonglide.io.LogSystem;

public import
	tango.io.Stdout,
	tango.io.Console,

	tango.util.log.Log,
	tango.util.log.AppendConsole,
	tango.util.log.AppendFile,
	tango.util.log.LayoutDate,
	tango.util.log.model.ILogger,

	tango.text.convert.Format;



//================================================================
/// basic loggers for debugging, tracing (main) and errors. All logs write to console.
/// usage: traceLog.trace( "message" ) / .info / .warn / .error / .fatal
//================================================================
Logger traceLog;
private Logger _errorLog;

private AppendFile
	traceFile,
	errorFile;

debug Logger debugLog;
debug AppendFile debugFile;


//================================================================
/// static module constructor creates a log that writes to log file and console
//================================================================
static this()
{
	// all loggers, as children of root, print also to console by default
	Log.root.add( new AppendConsole( new LayoutDate ) );
	// trace is the lowest level, so all levels are logged
	Log.root.level( Logger.Level.Trace );

	traceLog = Log.getLogger( "trace" );
	_errorLog = Log.getLogger( "error" );
	debug debugLog = Log.getLogger( "debug" );

	traceFile = new AppendFile( "trace.log", new LayoutDate );
	errorFile = new AppendFile( "error.log", new LayoutDate );
	debug debugFile = new AppendFile( "debug.log", new LayoutDate );

	traceLog.add( traceFile );
	_errorLog.add( errorFile );
	debug debugLog.add( debugFile );

	version( Windows ) GetConsoleHandle();
}

static ~this()
{
	traceFile.close;
	errorFile.close;
	debug debugFile.close;

	delete traceLog;
	delete _errorLog;
	debug delete debugLog;
}

version( Windows )
{
	import tango.sys.win32.UserGdi;

	HANDLE hStdout, hStdin;
	WORD wOldColorAttrs;

	//================================================================
	/// get stdout handle for changing console output text color
	//================================================================
	bool GetConsoleHandle()
	{
		CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

		hStdin = GetStdHandle( STD_INPUT_HANDLE );
		hStdout = GetStdHandle( STD_OUTPUT_HANDLE );

		if( hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE )
		{
			_errorLog.error( "GetStdHandle error" );
			return false;
		}

		if( !GetConsoleScreenBufferInfo( hStdout, &csbiInfo ))
		{
			_errorLog.error( "GetConsoleScreenBufferInfo" );
			return false;
		}

		wOldColorAttrs = csbiInfo.wAttributes;

		SetConsoleScreenBufferSize (hStdout,COORD(1000,3000));

		return true;
	}


	void errorLog( char[] msg )
	{
		SetConsoleTextAttribute( hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY );
		_errorLog.error( msg );
		SetConsoleTextAttribute( hStdout, wOldColorAttrs );
	}

	void attentionLog( char[] msg )
	{
		SetConsoleTextAttribute( hStdout, FOREGROUND_INTENSITY );
		traceLog.trace( msg );
		SetConsoleTextAttribute( hStdout, wOldColorAttrs );
	}
	void attentionLogR( char[] msg )
	{
		SetConsoleTextAttribute( hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY );
		traceLog.trace( msg );
		SetConsoleTextAttribute( hStdout, wOldColorAttrs );
	}
	void attentionLogB( char[] msg )
	{
		SetConsoleTextAttribute( hStdout, FOREGROUND_BLUE|FOREGROUND_INTENSITY );
		traceLog.trace( msg );
		SetConsoleTextAttribute( hStdout, wOldColorAttrs );
	}
	void attentionLogG( char[] msg )
	{
		SetConsoleTextAttribute( hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY );
		traceLog.trace( msg );
		SetConsoleTextAttribute( hStdout, wOldColorAttrs );
	}
	debug
	{
            void debugLogB( char[] msg )
            {
                SetConsoleTextAttribute( hStdout, FOREGROUND_BLUE|FOREGROUND_INTENSITY );
                debugLog.trace( msg );
                SetConsoleTextAttribute( hStdout, wOldColorAttrs );
            }
            void debugLogR( char[] msg )
            {
                SetConsoleTextAttribute( hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY );
                debugLog.trace( msg );
                SetConsoleTextAttribute( hStdout, wOldColorAttrs );
            }
            void debugLogG( char[] msg )
            {
                SetConsoleTextAttribute( hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY );
                debugLog.trace( msg );
                SetConsoleTextAttribute( hStdout, wOldColorAttrs );
            }
            void debugLogY( char[] msg )
            {
                SetConsoleTextAttribute( hStdout, FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY );
                debugLog.trace( msg );
                SetConsoleTextAttribute( hStdout, wOldColorAttrs );
            }
       	}

}
else
{
	void errorLog( char[] msg )
	{
		_errorLog.error( msg );
	}

	void attentionLog( char[] msg )
	{
		traceLog.trace( msg );
	}

	void attentionLogR( char[] msg )
	{
		traceLog.trace( msg );
	}

	void attentionLogB( char[] msg )
	{
		traceLog.trace( msg );
	}

	void attentionLogG( char[] msg )
	{
		traceLog.trace( msg );
	}

	debug
	{
		void debugLogB( char[] msg )
		{
			debugLog.trace( msg );
		}

		void debugLogR( char[] msg )
		{
			debugLog.trace( msg );
		}

		void debugLogG( char[] msg )
		{
			debugLog.trace( msg );
		}

		void debugLogY( char[] msg )
		{
			debugLog.trace( msg );
		}
	}
}
